from django.template.loader import get_template
from django.template import Context
from django.http import Http404, HttpResponse
from django.shortcuts import render_to_response
import datetime

def hello(request):
	return HttpResponse("Hello World!")

def current_datetime(request):
	now = datetime.datetime.now()
	#html = '<html><body>Now is %s.</body></html>' % now
	t = get_template('current_datetime.html')
	html = t.render(Context({'current_date': now}))
	return HttpResponse(html)

def hours_ahead(request, offset):
	try:
		offset = int(offset)
	except ValueError:
		raise Http404()
	dt = datetime.datetime.now() + datetime.timedelta(hours=offset)
	html = 'In %s hour(s), it will be %s.' % (offset, dt)
	return HttpResponse(html)

def display_meta(request):
        values = request.META.items()
        values = sorted(values)
        return render_to_response('display_meta.html', {'values': values})

def search_form(request):
        return render_to_response('search_form.html')

def search(request):
        if 'q' in request.GET and request.GET['q']:
                message = 'You search for: %r' % request.GET['q']
                #%r是打印内存格式的意思。
        else:
                #message = 'You submitted an empty form.'
                return render_to_response('search_form.html', {'error': True})
        return HttpResponse(message)
